<template>
  <el-drawer v-model="drawerVisible" :title="drawerTitle" direction="rtl" size="80%" class="script">
    <el-form :model="form" ref="formRef" label-width="150px"  label-position="top" :rules="formRules" size="small" class="custom-scrollbar">
      <el-form-item label="脚本操作名称" style="width: 100%;" prop="name">
        <el-input v-model="form.name" placeholder="请输入脚本操作名称" />
      </el-form-item>
    </el-form>
    <CustomScript @change-editor="changeEditor" />
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">添 加</el-button>
      <el-button @click="drawerVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import CustomScript from '../../components/prepost/CustomScript.vue'
const drawerVisible = ref(false)
const drawerTitle = ref('脚本操作')
const formRef = ref()
const formLoading = ref(false)
const form = reactive({
  name: '',
  content: ''
})
const formRules = reactive({
  name: [{ required: true, message: '脚本操作名称不能为空', trigger: 'blur' }]
})
const emit = defineEmits(['success'])

/** 打开弹窗 */
const open = async (row?: any) => {
  drawerVisible.value = true
  resetForm()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
/** 重置form */
const resetForm = () => {

}
/** editor内容更新 */
const changeEditor = (content) => {
  form.content = content
}
/** 提交 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid || !form.content) return
  formLoading.value = true
  emit('success',form)
  drawerVisible.value = false
  formLoading.value = false
}
</script>

<style scoped></style>
